package utilities.nonessential;

import exec.LoadAndExecute;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class massTester {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File input = new File(args[0]);//absolute path to test.IOFILES
		File output = new File(args[1]);//absolute path to output directory
		try{
		iterate(input, output);
		}
		catch(Exception e){
			
		}
	}

	private static void iterate(File in, File out) throws IOException {
		if(in.getName()=="empty_proc_inp"){
			return;
		}
		if (in.isDirectory())
			for (File child : in.listFiles()) {
				File newOut = new File(out.getAbsolutePath() + "/"
						+ in.getName());
				newOut.mkdir();
				iterate(child, newOut);
			}
		else {
			File outDir = new File(out.getAbsolutePath() + "/" + in.getName());
			outDir.mkdir();
			String outPath = outDir.getAbsolutePath();
			File execIn = new File(outPath + "/executable-input");
			execIn.createNewFile();
			copyFile(in, execIn);
			File procIn = new File(outPath + "/process-input");
			procIn.createNewFile();
			String[] argFiles = { outPath + "/executable-input",
					outPath + "/process-input", outPath + "/process-output",
					outPath + "/process-trace" };
			LoadAndExecute.main(argFiles);
			return;
		}
	}

	private static void copyFile(File orig, File copy) throws IOException {

			BufferedInputStream origStream = new BufferedInputStream(
					new FileInputStream(orig));
			BufferedOutputStream copyStream = new BufferedOutputStream(
					new FileOutputStream(copy));
			int next = origStream.read();
			while (next != -1) {
				copyStream.write(next);
				next = origStream.read();
			}
			origStream.close();
			copyStream.close();

	}
}
